extends Node

var utils = null
var ctrl = null
var diff
var base = null
var path = null
var window
var recordWindow
var shop
var research
var charaBuildBonus = 0
var shieldBonus = 1
var endlessopt:bool = false
func _ready():
	pass
func _init():
	call_deferred("init")
	var a = sys.get_node("/root/")
	a.call_deferred("add_child",self)
func _connect():
    pass
    
func init():
	if globalData.infoDs.has("g_AneHekBase"):
		base = globalData.infoDs["g_AneHekBase"]
		path = chaData.infoDs["cTDW"].dir
	if base != null:
		#初始化
		utils = globalData.infoDs["g_AneHekUtils"]
		ctrl = globalData.infoDs["g_AneHek"]
		base.connect("onStartGame",self,"initendless")
		
		#azurCtrl.connect("initMainUI", self, "initBonus")
		#diff = globalData.infoDs["g_AneHekDifficult"]
		#shop = globalData.infoDs["g_AneHekShop"]
		#research = globalData.infoDs["g_AneHekResearch"]
#—————————————————————————————————信号类—————————————————————————————————#
signal canchangemsg
var lcnt = sys.get_node("/root/topUi").get_child_count()
func _process(delta):
	if !ctrl.allflag:return
	#._upS()
	var ncnt = sys.get_node("/root/topUi").get_child_count()
	#print(lcnt,"   ",ncnt)
	if lcnt > ncnt:
		lcnt = ncnt
	if lcnt < ncnt:
		var uinode = sys.get_node("/root/topUi")
		for i in range(ncnt):
			var node = uinode.get_child(i)
			if node.get_child_count() >= 3:
				if node.name == "Control" && node.get_child(2).name == "NinePatchRect2":
					createchoose(node.get_child(2))
					lcnt = ncnt
					break
			if node.get("item") != null && node.item is Item:
				#print("send signal")
				lcnt = ncnt
				emit_signal("canchangemsg",node)
				#lcnt = 0
				break
#—————————————————————————————————信号类—————————————————————————————————#
#——————————————————————————————真·无尽开关与难度————————————————————————————————#
func initendless():
	count = 0
	sys.main.connect("onTongGuan", self, "afterTongGuan")
#	if sys.test:
#		sys.main.guankaMsg.lvStep =  159
#		sys.main.batLLv = 6
func createchoose(node):
	var opt = CheckBox.new()
	endlessopt = base.getSetting("endlessmode",false)
	opt.pressed = endlessopt
	node.add_child(opt)
	opt.set_position(Vector2(400,50))
	opt.text = "真·无尽"
	opt.connect("pressed",self,"run",[opt])

func run(opt):
	endlessopt = opt.pressed
	base.setSetting("endlessmode",endlessopt)
	#print(endlessopt)
var count = 0
var tmplist = []
func afterTongGuan(x):	
	#if count == 0:
	#	var maxDiff = int(base.getSetting("maxDiff", 1))
	#	var diff = int(base.getSetting("difficult", 1))
	#	if diff > maxDiff:
	#		base.setSetting("maxDiff", diff)
	#var setting = base.getSetting("enableEx", true)
	if !endlessopt || !ctrl.allflag:return
	yield(sys.get_tree().create_timer(1),"timeout")
	if sys.main.guankaMsg.lvStep < 100:return
	if utils.x1:
		base.setData("x1",utils.x1)
	sys.main.guankaMsg.lvStep = 2
	sys.main.isEnd = false
	sys.main.batLLv = 0
	sys.main.get_node("ui/guanKa/Button").setMap()
	sys.main.guankaMsg.nextLv()
	count += 1
	var maxdiff = int(base.getSetting("maxDiff",1))
	var diff = int(base.getSetting("difficult",1))
	if diff == maxdiff + 2:
		base.setSetting("maxDiff",maxdiff + 2) #改成解锁2难度
	#emit_signal("startEndlessMode", count)
	sys.newBaseMsg("通知","真无尽模式已开启,现在返回战场后可以点击右上角的选关按钮继续挑战，同时MOD设置里面已解锁更高难度;塔防英雄天赋已解锁新人物")
	sys.main.connect("onBattleReady", self, "setdiff")

#改变无尽难度
var reChas = []
func setdiff():
	sys.main.player.plusGold(1000)
	var chacnt = 20
	reChas = []
	reCell = Vector2(5,-1)
	var nstep:int = sys.main.guankaMsg.lvStep - 2 + count * 161
	ctrl.layertxt.text="当前层数:%d" % int(nstep)
	clearAll()
	if nstep < 161 + 27:
		pass
	elif nstep < 161 + 54:
		chacnt += 2
	elif nstep < 161 + 81:
		chacnt += 4
	elif nstep < 161 + 108:
		chacnt += 6		#boss lv1 27关 创建两个2级头目 66
	elif nstep < 161 + 135:
		chacnt += 8
	elif nstep < 161 + 161:
		chacnt += 10
	else:
		pass
	for i in range(chacnt):
		createChaForRE(sys.rndListItem(utils.x5))
	var n = 1
	if nstep <= 161 + 54:
		n = 1
	elif nstep <= 161 + 108:
		n = 2
	else:
		n = 3
	for i in range(n):
		for j in reChas:
			j.addItem(sys.newItem(sys.rndListItem(itemData.rndPool.items)[0].id))
		#boss lv2 54关 77

		#boss lv3 81关 78
 
		#boss lv4 108关 88

		#boss lv5 135关 89

		#boss lv6 161关 99
	
var reCell = Vector2(5,-1)
func createChaForRE(id):
	reCell += Vector2(0, 1)
	if reCell.y > 5:
		reCell.y = 0
		reCell.x += 1
	var cha = sys.main.newChara(id, 2)
	reChas.append(cha)
	sys.main.map.add_child(cha)
	sys.main.setMatCha(reCell, cha)
	yield(sys.get_tree().create_timer(0.02), "timeout")
	#if cha.get("type") == "BOSS":
	#	cha.delFlag = false
	#	cha.flag = false
func clearAll():
	for i in utils.getAllyCells(2):
		var cha = sys.main.matCha(i)
		if cha!=null && cha.team != 1 && cha.get("type") != "BOSS": 
			sys.main.delMatChara(cha)